package org.example.studyonline.controller;

import jakarta.annotation.Resource;
import org.example.studyonline.pojo.CourseMaterial;
import org.example.studyonline.pojo.DTO.AddChapterDTO;
import org.example.studyonline.pojo.DTO.AddCourseDTO;
import org.example.studyonline.pojo.DTO.DeleteCourseDTO;
import org.example.studyonline.pojo.DTO.UpdateCourseDTO;
import org.example.studyonline.pojo.PageBean;
import org.example.studyonline.pojo.Result;
import org.example.studyonline.pojo.VO.CourseListVO;
import org.example.studyonline.pojo.VO.StatisticsVO;
import org.example.studyonline.pojo.VO.UserListVo;
import org.example.studyonline.pojo.VO.WatchCountVO;
import org.example.studyonline.service.ChapterService;
import org.example.studyonline.service.CourseMaterialService;
import org.example.studyonline.service.CourseService;
import org.example.studyonline.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private CourseService courseService;

    @Resource
    private UserService userService;

    @Resource
    private ChapterService chapterService;

    @Resource
    private CourseMaterialService courseMaterialService;


    // 修改课程信息
    @PutMapping("/course")
    public Result course(@RequestBody @Validated UpdateCourseDTO updateCourseDTO){
        courseService.update(updateCourseDTO);
        return Result.success(null);
    }

    // 修改课程信息
    @PostMapping("/course")
    public Result course(@RequestBody @Validated AddCourseDTO addCourseDTO){
        courseService.add(addCourseDTO);
        return Result.success(null);
    }

    // 删除课程信息
    @DeleteMapping("/course")
    public Result course(@RequestBody @Validated DeleteCourseDTO deleteCourseDTO){
        courseService.delete(deleteCourseDTO);
        return Result.success(null);
    }


    @PutMapping("/user/freeze/{userID}")
    public Result freezeUserByUserID(@PathVariable Integer userID){
        userService.freezeUserByUserID(userID);
        return Result.success("用户冻结成功","{}");
    }

    @PostMapping("/course/chapter")
    public Result addChapter(@RequestBody @Validated AddChapterDTO addChapterDTO){
        chapterService.add(addChapterDTO);
        return Result.success("章节添加成功",null);
    }

    @GetMapping("/user/list")
    public Result<PageBean<UserListVo>> getUserList(@RequestParam(value = "page", defaultValue = "1") Long pageNo, @RequestParam(value = "pageSize", defaultValue = "2") Long pageSize){
        PageBean<UserListVo> list = new PageBean<>();
        list =userService.getUserList(pageNo,pageSize);
        if (list == null) {
            return Result.error("获取用户列表失败");
        }
        return Result.success("获取用户列表成功",list);
    }

    @GetMapping("/course/statistics")
    public Result<PageBean<StatisticsVO>> getStatistics(@RequestParam(value = "page", defaultValue = "1") Long pageNo, @RequestParam(value = "pageSize", defaultValue = "2") Long pageSize){
        PageBean<StatisticsVO> pageBean = courseService.getStatistics(pageNo,pageSize);
        return Result.success("获取统计数据成功",pageBean);
    }

    @GetMapping("/course/watch-count/")
    public Result getWatchCount(@RequestParam(value = "page", defaultValue = "1") Long pageNo, @RequestParam(value = "pageSize", defaultValue = "2") Long pageSize){
        PageBean<WatchCountVO> list = chapterService.getWatchCount(pageNo,pageSize);
        return Result.success("获取观看次数成功",list);
    }

    @PostMapping("/chapter/material/put")
    public Result putMaterial(@RequestParam("chapterID") Integer chapterID,@RequestParam("type")String type,@RequestParam("filePath")String filePath){
        courseMaterialService.putMaterial(chapterID,type,filePath);
        return Result.success();
    }

    @PostMapping("/chapter/material/update")
    public Result updateMaterial(@RequestParam("materialID")Integer materialID,@RequestParam("chapterID") Integer chapterID,@RequestParam("type")String type,@RequestParam("filePath")String filePath){
        courseMaterialService.updateMaterial(materialID,chapterID,type,filePath);
        return Result.success();
    }

    @PostMapping("/chapter/material/delete")
    public Result deleteMaterial(@RequestParam("materialID")Integer materialID){
        courseMaterialService.removeById(materialID);
        return Result.success();
    }
}
